﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.WebModel;
using TravelLink.DAL;

namespace TravelLink.WebApp.UserControl.Member {
    public partial class UC_Member_PositionManager : ControlBase {

        private int _pageCollectSize = 15;
        private int _pageApplySize = 15;
        public int CollectPageSize { get { return _pageCollectSize; } set { _pageCollectSize = value; } }
        public int ApplyPageSize { get { return _pageApplySize; } set { _pageApplySize = value; } }

        protected void Page_Load(object sender, EventArgs e) {


            if (WebHelper.GetQueryStringStr("action", "").Equals
                ("cancel", StringComparison.CurrentCultureIgnoreCase)) {

                string code = WebHelper.GetQueryStringStr("code", Guid.Empty.ToString());
                Guid pCode = Guid.Empty;
                if (Guid.TryParse(code, out pCode)) {
                    this.DB.TL_User_Position_Collect.DeleteAllOnSubmit(this.DB.TL_User_Position_Collect.Where(u => u.PositionCode == pCode && u.UserCode == this.CurrentUser.DBUser.UserCode));
                    this.DB.SubmitChanges();
                }

            }

            var dataApplyList = (from position in this.DB.TL_Company_Position
                                 join apply in this.DB.TL_User_Position_Collect
                                 on position.PositionCode equals apply.PositionCode
                                 where apply.ApplyDate.HasValue && apply.UserCode == this.CurrentUser.DBUser.UserCode
                                 select position);
            //dataList[0].TL_City.CName

            int currentAPage = 1;
            int.TryParse(HttpContext.Current.Request.QueryString["p_a"], out currentAPage);
            if (currentAPage < 1) {
                currentAPage = 1;
            }
            UC_Pager2.PageIndex = currentAPage;
            UC_Pager2.JumpUrl = this.GetPageJumpUrl("p_a");



            UC_Pager2.NoteTotal = dataApplyList.Count();
            UC_Pager2.PageSize = _pageApplySize;

            this.rptDataSource.DataSource = dataApplyList.Skip((currentAPage - 1) * _pageApplySize).Take(_pageApplySize).ToList();
            this.rptDataSource.DataBind();





            var dataCollectList = (from position in this.DB.TL_Company_Position
                                   join apply in this.DB.TL_User_Position_Collect
                                   on position.PositionCode equals apply.PositionCode
                                   where apply.CollectDate.HasValue && apply.UserCode == this.CurrentUser.DBUser.UserCode
                                   select position);
            //dataList[0].TL_City.CName


            UC_Pager1.NoteTotal = dataCollectList.Count();
            UC_Pager1.PageSize = _pageCollectSize;
            int currentCPage = 1;
            int.TryParse(HttpContext.Current.Request.QueryString["p_c"], out currentCPage);
            if (currentCPage < 1) {
                currentCPage = 1;
            }
            UC_Pager1.PageIndex = currentAPage;
            UC_Pager1.JumpUrl = this.GetPageJumpUrl("p_c");

            this.rptCollectDataSource.DataSource = dataCollectList.Skip((currentCPage - 1) * _pageCollectSize).Take(_pageCollectSize).ToList();
            this.rptCollectDataSource.DataBind();
        }


        protected string BindCity(object dataItem) {
            if (dataItem is TL_Company_Position) {
                var source = (dataItem as TL_Company_Position);
                if (source != null && source.TL_City != null) {
                    return source.TL_City.CName;
                }
                return string.Empty;
            }
            return string.Empty;
        }

        protected string BindPositionStatus(object dataItem) {
            if (dataItem is TL_Company_Position) {
                var source = (dataItem as TL_Company_Position);
                if (source != null) {
                    return Enum_PositionStatus.Resolve(source.Status);
                }
                return string.Empty;
            }
            return string.Empty;
        }
    }
}